import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
import vuexPersist from "vuex-persist";
export default new Vuex.Store({
  state: {
    shopcar: [],
  },
  mutations: {
    add(state, item) {
      // state.shopcar.push(item);
      var flag = false;
      state.shopcar.forEach((i) => {
        if (item._id == i._id) {
          i.num++;
          flag = true;
        }
      });

      if (flag == false) {
        item.num = 1;
        state.shopcar.push(item);
      }
    },
    changenum() {},
    remove(state, i) {
      state.shopcar.splice(i, 1);
    },
  },
  actions: {},
  modules: {},
  plugins: [
    new vuexPersist({
      localstorage: window.localStorage,
    }).plugin,
  ],
});
